package cn.htsyb.synergy.utils;

import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @since 1.0
 * @date 2022/5/11
 */
public class LangUtil {
    static Pattern numberPattern = Pattern.compile("^-?[0-9]+");
    static Pattern floatPattern = Pattern.compile("^-?[0-9]+\\.?[0-9]+");

    /**
     * 判断字符串是否为整数
     *
     * @param context
     * @return
     */
    public static boolean isNumeric(String context) {
        Matcher isNum = numberPattern.matcher(context);
        return isNum.matches();
    }

    /**
     * 判断字符串是否为浮点数
     *
     * @param context
     * @return
     */
    public static boolean isFloat(String context) {

        Matcher isFloat = floatPattern.matcher(context);
        return isFloat.matches();
    }

    /**
     * 合并集合
     *
     * @param arrays 多个集合对象
     * @param <T>    多个集合
     * @return
     */
    public static <T> Collection<T> mergeArray(Collection<T>... arrays) {
        Collection<T> tagCollect = null;
        for (Collection<T> entity : arrays) {
            if (tagCollect == null) {
                tagCollect = entity;
            } else {
                if (entity != null) {
                    tagCollect.addAll(entity);
                }
            }
        }
        return tagCollect;
    }

    /**
     * 模余次数
     *
     * @param totalNum
     * @param pageNum
     * @return
     */
    public static int tryModNum(int totalNum, int pageNum) {
        if (totalNum < pageNum) {
            return 1;
        } else {
            return (totalNum / pageNum) + (totalNum % pageNum > 0 ? 1 : 0);
        }
    }

    /**
     * 将多个非空对象拼装成字符串
     * 主要解决空对象拼装后显示为null问题
     *
     * @param bool
     * @param separator
     * @param objArr
     * @return
     */

    public String appendToString(boolean bool, String separator, Object... objArr) {
        StringBuffer buffer = new StringBuffer();
        if (bool) {
            appendToString(separator, objArr);
        }
        return buffer.toString();
    }

    /**
     * 将多个非空对象拼装成字符串
     *
     * @param separator
     * @param objArr
     * @return
     */
    public String appendToString(String separator, Object... objArr) {
        StringBuffer buffer = new StringBuffer();
        for (Object obj : objArr) {
            buffer.append(appendToString(obj)).append(separator);
        }
        return buffer.toString();
    }

    /**
     * 将非空对象拼装成字符串
     * 主要解决空对象拼装后显示为null问题
     *
     * @param obj
     * @return
     */
    public String appendToString(Object obj) {
        if (obj == null) {
            return "";
        } else if (obj instanceof Class) {
            return obj.toString();
        } else {
            return String.valueOf(obj);
        }
    }

}
